import os

print("使用说明：")
print("    将需要检查的以【nnn】三位数字命名的文件夹放入任意一个文件夹中，将该文件夹拖入下方")
print("    所有错误都会输出在下方，该程序不会更改与生成任何文件")

dirpath = input("拖入文件夹：")
#dirpath = r"C:\caoli\pycharm\project1\duhaiyang\check"#测试文本
dirnames = ["DynamicGesture","StaticGesture","LipDetect"]

DGfiles = ['A_IR_Disturb_1','A_IR_Disturb_2','A_IR_Normal','A_IR_Trigger',
           'Rearview_IR_Disturb_1','Rearview_IR_Disturb_2','Rearview_IR_Normal','Rearview_IR_Trigger',
           'Rearview_RGB_Disturb_1','Rearview_RGB_Disturb_2','Rearview_RGB_Normal','Rearview_RGB_Trigger',
           'Whell_IR_Disturb_1','Whell_IR_Disturb_2','Whell_IR_Normal','Whell_IR_Trigger']

SGfiles = ['A_IR_Disturb','A_IR_Hard','A_IR_Normal',
           'Rearview_IR_Disturb','Rearview_IR_Disturb','Rearview_IR_Hard',
           'Rearview_IR_Hard','Rearview_IR_Normal','Rearview_IR_Normal',
           'Rearview_RGB_Disturb','Rearview_RGB_Disturb','Rearview_RGB_Hard',
           'Rearview_RGB_Hard','Rearview_RGB_Normal','Rearview_RGB_Normal',
           'Whell_IR_Disturb','Whell_IR_Hard','Whell_IR_Normal']

LDfiles = ['A_IR_1','A_IR_2','A_IR_3','A_IR_4','A_IR_5','A_IR_6','A_IR_7',
           'Rearview_IR_1','Rearview_IR_2','Rearview_IR_3','Rearview_IR_4','Rearview_IR_5','Rearview_IR_6','Rearview_IR_7',
           'Rearview_RGB_1','Rearview_RGB_2','Rearview_RGB_3','Rearview_RGB_4','Rearview_RGB_5','Rearview_RGB_6','Rearview_RGB_7',
           'Whell_IR_1','Whell_IR_2','Whell_IR_3','Whell_IR_4','Whell_IR_5','Whell_IR_6','Whell_IR_7']

def dir3check(dirs):
    if set(dirs) == set(dirnames):
        return True
    else:
        return False

def filelencheck(dirfilename,filelist):
    if dirfilename == "DynamicGesture":
        if len(filelist) == 16:
            return True
        else:
            return False
    if dirfilename == "StaticGesture":
        if len(filelist) == 18:
            return True
        else:
            return False
    if dirfilename == "LipDetect":
        if len(filelist) == 28:
            return True
        else:
            return False


def filenamecheck(dirfilename,midlist):
    #print("_".join(midlist))
    if dirfilename == "DynamicGesture":
        #if midlist[-2] == "Disturb":

        if "_".join(midlist) in DGfiles:
            return True
        else:
            print(1)
            return False
    if dirfilename == "StaticGesture":

        if "_".join(midlist) in SGfiles:
            return True
        else:

            print(2,"StaticGesture")
            return False
    if dirfilename == "LipDetect":

        if "_".join(midlist) in LDfiles:
            return True
        else:
            #print(3,"LipDetect")
            return False

def filemidcheck(filenamelist,num,nn,root,file):
    #print(filenamelist,num)
    try:
        if filenamelist[nn] != num:
            print(1,filenamelist)
            return False
        filemidflag = filenamecheck(filenamelist[0], filenamelist[1:nn])
        if filemidflag == False:
            print(2,"filemidflag")
            return False
    except Exception:
        print(root,file,"文件命名格式不规范")
        return False

def run(dirpath):
    dir3flag = False
    fileflag = False
    filelenflag = True
    filemidflag = True
    for root,dirs,files in os.walk(dirpath):
        #print(dirpath[-3:])
        num = dirpath[-3:]#check-编号-子文件夹
        #print("1num",num,dirpath)
        if dirs != []:
            dir3flag = dir3check(dirs)
            if dir3flag == False:
                print(dirpath,"第一层三个文件夹错误")
                return False,1
            #print(dirs,dir3flag)
        else:
            if dir3flag == False:
                print(dirpath, "文件夹缺失")
                return False,5

        if files != []:
            fileflag = True
            dirfilename = files[0].split("_")[0]
            filelenflag = filelencheck(dirfilename,files)

            if filelenflag == False:
                print(dirpath, dirfilename,"该文件夹内文件数目错误")
                return False,2

            RLset = set()
            n1_n2=["A_IR_Disturb","Rearview_IR_Disturb","Rearview_RGB_Disturb","Whell_IR_Disturb"]
            for file in files:
                file = file.split(".")[0]
                filenamelist = file.split("_")
                if filenamelist[-1] == "1" or filenamelist[-1] == "2":
                    number = filenamelist[-3]
                    times = filenamelist[-2]
                    if len(number) != 3 or len(times) != 12:
                        print(os.path.join(root,file),"编号或时间戳长度错误")

                    if "_".join(filenamelist[1:4]) in n1_n2:
                        continue
                    else:
                        print("_".join(filenamelist[1:4]))
                        return False

                if filenamelist[-1] == "rear":

                    if filemidcheck(filenamelist, num, -4,root,file) == False:
                        print(root,file,"文件编号与文件夹编号不符")
                        return False,3
                    RLset.add(filenamelist[-2])
                else:
                    if filemidcheck(filenamelist, num, -2,root,file) == False:
                        print(root, file, "文件编号与第一层文件夹命名不符")
                        return False,4
            if len(RLset) > 1:
                print(root,'StaticGesture 文件夹内‘left’,‘right’同时出现')
                return False


    if fileflag == False:
        print(dirpath,"文件夹内文件缺失")
        return False

    print(dirpath,"OK")
    return True,0

for dir in os.listdir(dirpath):
    aaa = os.path.join(dirpath,dir)
    #print(aaa)
    run(aaa)

a = input("回车退出")